# -*- coding: utf-8 -*-
"""
Created on 2024-03-20 17:31:24
---------
@summary:
---------
@author: 11450
"""
import time

import feapder
from random import randint

from lxml import etree


class Tourism(feapder.AirSpider):

    def download_midware(self, request):
        request.headers = {
            'Referer': 'https://www.mafengwo.cn/i/5376978.html',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'
        }

        return request

    def start_requests(self):
        yield feapder.Request("https://www.mafengwo.cn/gonglve/", page=1)

    def parse(self, request, response):
        html = etree.HTML(response.text)

        div_list = html.xpath(
            '//div[@class="_j_feed_data"]/div[@class="feed-item _j_feed_item"]'
        )
        for div in div_list:
            item = dict()
            item['star'] = div.xpath(
                './/a/div[@class="bar clearfix"]/span/span/text()')[0].strip()
            item['title'] = div.xpath('.//a/div[@class="title"]/text()')[0].strip()
            item['type'] = div.xpath('.//a//span[@class="type"]/strong/text()')[0].strip()
            try:
                if item['type'] == '游记' or item['type'] == '官方账号':
                    item['content'] = div.xpath('.//a//div[@class="info"]/text()')[0].strip()
                    item['author'] = div.xpath('.//a//div[@class="ext-r"]/span/text()')[0].strip()
                    item['nums'] = div.xpath('.//a//div[@class="ext-r"]/span[last()]/text()')[0].strip()
                else:
                    item['content'] = div.xpath('.//a/div/div[@class="info"]/text()')[0].strip()

                    item['nums'] = div.xpath('.//a/div[@class="imgs"]//li[@class="ext-r"]/text()')[0].strip()
            except Exception:
                print("错误")
            print(item)

        # 翻页
        request.page += 1
        if request.page < 10:
            yield feapder.Request("https://www.mafengwo.cn/gonglve/", data={"page": request.page},
                                  page=request.page,
                                  callback=self.parse)



if __name__ == "__main__":
    Tourism().start()
